package com.cxria.script

import com.cxria.script.upload.domain.AudioInfo
import com.cxria.script.upload.domain.FormatInfo
import com.cxria.script.upload.domain.Job
import com.cxria.script.upload.domain.Resource
import com.cxria.script.upload.domain.TranscodeInfo
import com.cxria.script.upload.domain.Video
import com.cxria.script.upload.domain.VideoInfo;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

@Singleton(lazy = true)
class HibernateUtils{

	SessionFactory sessionFactory

	SessionFactory getSessionFactory() {
		if(sessionFactory == null){
			Configuration cfg = new Configuration();
//			cfg.configure("hibernate.cfg.xml");
			cfg.setProperty('hibernate.connection.driver_class', 'com.mysql.jdbc.Driver')
			.setProperty('hibernate.connection.url', 'jdbc:mysql://localhost:3306/ngaiamount?useUnicode=true&characterEncoding=UTF-8')
			.setProperty('hibernate.connection.username', 'root')
			.setProperty('hibernate.connection.password', 'maomao')
			.setProperty('hibernate.dialect', 'org.hibernate.dialect.MySQL57Dialect')
			.setProperty('hibernate.show_sql', 'true')
			.setProperty('hibernate.hbm2ddl.auto', 'update')
			.addAnnotatedClass(Job.class)
			.addAnnotatedClass(AudioInfo.class)
			.addAnnotatedClass(FormatInfo.class)
			.addAnnotatedClass(Resource.class)
			.addAnnotatedClass(TranscodeInfo.class)
			.addAnnotatedClass(Video.class)
			.addAnnotatedClass(VideoInfo.class)
			sessionFactory = cfg.buildSessionFactory();
		}
		sessionFactory
	}

	void close(){
		if ( sessionFactory != null ) {
			sessionFactory.close();
		}
	}

}
